package cn.silver.module.web.service;

import cn.silver.framework.core.constant.GlobalConstants;
import cn.silver.framework.core.exception.user.CaptchaException;
import cn.silver.framework.core.exception.user.CaptchaExpireException;
import cn.silver.framework.core.exception.user.UserException;
import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.redis.utils.RedisUtils;
import cn.silver.framework.web.properties.CaptchaProperties;
import cn.silver.module.system.param.UserParam;
import cn.silver.module.system.service.ISysUserService;
import cn.silver.module.web.model.RegisterModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 注册校验方法
 *
 * @author Silver Zhou
 */
@RequiredArgsConstructor
@Service
public class SysRegisterService {

    private final ISysUserService userService;
    private final CaptchaProperties captchaProperties;

    /**
     * 注册
     */
    public void register(RegisterModel registerBody) {
        String tenantId = registerBody.getTenantId();
        String username = registerBody.getUsername();
        String password = registerBody.getPassword();
        // 校验用户类型是否存在
//        String userType = UserType.getUserType(registerBody.getUserType()).getUserType();

        boolean captchaEnabled = captchaProperties.getEnable();
        // 验证码开关
        if (captchaEnabled) {
            validateCaptcha(tenantId, username, registerBody.getCode(), registerBody.getUuid());
        }
        UserParam sysUser = new UserParam();
        sysUser.setCode(username);
        sysUser.setName(username);


//        if (!userService.checkUserNameUnique(sysUser)) {
//            throw new UserException("user.register.save.error", username);
//        }
        boolean regFlag = userService.registerUser(sysUser, tenantId);
        if (!regFlag) {
            throw new UserException("user.register.error");
        }
//        recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.register.success"));
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     */
    public void validateCaptcha(String tenantId, String username, String code, String uuid) {
        String verifyKey = StringUtils.defaultString(uuid, "");
        String captcha = RedisUtils.getCacheObject(GlobalConstants.CAPTCHA_CODE_KEY, verifyKey);
        RedisUtils.deleteObject(GlobalConstants.CAPTCHA_CODE_KEY, verifyKey);
        if (captcha == null) {
//            recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire"));
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
//            recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error"));
            throw new CaptchaException();
        }
    }

    /**
     * 记录登录信息
     *
     * @param tenantId 租户ID
     * @param username 用户名
     * @param status   状态
     * @param message  消息内容
     * @return
     */
//    private void recordLogininfor(String tenantId, String username, String status, String message) {
//        LoginEvent loginEvent = new LoginEvent();
//        loginEvent.setTenantId(tenantId);
////        loginEvent.setUsername(username);
////        loginEvent.setStatus(status);
//        loginEvent.setMessage(message);
//        loginEvent.setRequest(ServletUtils.getRequest());
//        SpringUtils.context().publishEvent(loginEvent);
//    }

}
